<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of deperditionRoof
 *
 * @author TONY
 */
class AlgoRoof {

    public $te; //Température extérieure
    public $ti; //Température ambiante inrérieure
    public $textra; //Extrapolation - supplément de régulation
    public $tc = 365; //Durée de la période de calcul
    public $totalSurfaceRoof; //Surface totale du toit
    public $valueURoof; //Valeur U du toit
    public $SRE; //Surface de référence énergétique SRE
    public $totalDeperditionRoof; //Déperdition totale du toit

    function getTotalDepertitionRoof($house) {

        //Récupération de la température extérieure selon la ville de résidence
        $this->te = DBAccess::getExTemp($house->idCity);
        //Récupération de la température intérieure de la maison
        $this->ti = $house->roomsTemp;
        //Récupération du supplément de régulation pour la température ambiante
        $this->textra = DBAccess::getTempExtrapolation($house->year);
        //Température exemple
        $this->ti = $this->ti + $this->textra;
        //Calcul de la surface totale du toit
        $this->totalSurfaceRoof = $this->getTotalSurfaceRoof($house);
        //Récupération de la valeur U
        $this->valueURoof = $this->getValueURoof($house);
        //Calcul de la surface de référence énergétique
        $this->SRE = $this->getSRE($house);
  
        //Calcul de la déperdition pour le toit
        $this->totalDeperditionRoof = ($this->ti - $this->te) * $this->tc * $this->totalSurfaceRoof * $this->valueURoof * 86400 / (1000000 * $this->SRE);

        return $this->totalDeperditionRoof;
    }
    
    //Calcul de la valeur U du toit avec gestion des couches et de l'année de rénovation
    function getValueURoof($house){
      
            if (empty($house->roof->layers)) {
                if(empty($house->roof->year)){
                    return DBAccess::getValueURoofDB($house->year);
                }
                else{
                    return DBAccess::getValueURoofDB($house->roof->year);
                }
                
            }
            else{
                return 1/(0.14 + getRtermRoof($house->roof->layers));
            }
    }
    
    //Calcul de la résistance thermique des couches du toit
    // ex: {[0]=cm, [1]=matériel}
    function getRthermRoof($layers){
         $Rtherm = 0;

        //Parcours toutes les couches et aditionne les valeurs Rtherm
        foreach ($layers as $value) {

            $Rtherm += $value[0] / 100 / DBAccess::getRthermRoofMat("'" . utf8_decode($value[1]) . "'"); //récupération dans la BD
        }
        return $Rtherm;
    }
    
//Calcul de la surface totale du toit selon la forme de la maison et celle du toit
    function getTotalSurfaceRoof($house) {
        //Toit plat
        if ($house->roof->idShape == 7) {
            //Calcul de la surface normale du toit
            $this->totalSurface = getSurfaceFlatRoof($house);
        } else {
            
            switch ($house->idShape) {

                // Forme carré 
                case 1:
                    //Toit en pente
                    if ($house->roof->idShape == 1) {
                         return $house->M1 * sqrt(($house->M1 + ($house->roof->height * $house->roof->height)));
                    } else

                    //Toit forme B
                    if ($house->roof->idShape == 2 || $house->roof->idShape == 3 || $house->roof->idShape == 4) {
                        return 2 * $house->M1 * sqrt((($house->M1 / 2) * ($house->M1 / 2)) + ($house->roof->height * $house->roof->height));
                    } else

                    //Toit forme C
                    if ($house->roof->idShape == 5) {

                        return 2 * $house->M1 * sqrt(($house->M1 / 2) + ($house->roof->height * $house->roof->height));
                    } else

                    //Toit forme D
                    if ($house->roof->idShape == 6) {
                        return 1.144 * (2 * $house->M1 * sqrt((($house->M1 / 2) * ($house->M1 / 2)) + ($house->roof->height * $house->roof->height)));
                    }

                    break;
                // Forme rectangle 
                case 2:
                    
                    //Toit en pente
                    if ($house->roof->idShape == 1) {
                        return $house->M2 * sqrt((pow($house->M1, 2) + ($house->roof->height * $house->roof->height)));
                    } else
                        
                    //Toit forme B
                    if ($house->roof->idShape == 2 || $house->roof->idShape == 3 || $house->roof->idShape == 4) {
                        return 2 * $house->M2 * sqrt((($house->M1 / 2) * ($house->M1 / 2)) + ($house->roof->height * $house->roof->height));
                    } else
                        
                    //Toit forme D
                    if ($house->roof->idShape == 6) {
                        return 1.144 * (2 * $house->M2 * sqrt((($house->M1 / 2) * ($house->M1 / 2)) + ($house->roof->height * $house->roof->height)));
                    }
                    break;

                //Forme en L
                case 3:
                    //Toit en pente
                    if ($house->roof->idShape == 1) {
                        //Calcul de valeurs pour la surface
                        $house->roof->A1 = ($house->M2 - $house->M1) * sqrt(($house->roof->height*$house->roof->height) + ($house->M3*$house->M3));
                        $house->roof->A2 = ($house->M4 * sqrt(($house->roof->height*$house->roof->height) + ($house->M3*$house->M3))) / 2;
                        $house->roof->A3 = ($house->M3 * sqrt(($house->roof->height*$house->roof->height) + ($house->M4*$house->M4))) / 2;
                        $house->roof->A4 = $house->M1 - ($house->M3 * sqrt(($house->roof->height*$house->roof->height) + $house->M4));
                        return $house->roof->A1 + $house->roof->A2 + $house->roof->A3 + $house->roof->A4;
                    } else
                        
                    //Toit forme B
                    if ($house->roof->idShape == 2 || $house->roof->idShape == 3 || $house->roof->idShape == 4) {
                        //Calcul de valeurs pour la surface
                        $house->roof->A1 = 2 * (($house->M1 - $house->M3) * sqrt(($house->M4/2)*($house->M4/2) + ($house->roof->height*$house->roof->height)));
                        $house->roof->A2 = 2 * (($house->M2 - $house->M4) * sqrt(($house->M3/2)*($house->M3/2) + ($house->roof->height*$house->roof->height)));
                        $house->roof->A3 = ($house->M2 - ($house->M4/2)) * sqrt(($house->M3/2)*($house->M3/2) + ($house->roof->height*$house->roof->height));
                        $house->roof->A4 = ($house->M1 - ($house->M3/2)) * sqrt(($house->M4/2)*($house->M4/2) + ($house->roof->height*$house->roof->height));
                        $house->roof->A5 = ($house->M3 / (2 * sqrt(($house->M4/2)*($house->M4/2) + ($house->roof->height*$house->roof->height)))) / 2;
                        $house->roof->A6 = $house->roof->A5;
                        $house->roof->A7 = ($house->M4 / (2 * sqrt(($house->M3/2)*($house->M3/2) + ($house->roof->height*$house->roof->height)))) / 2;
                        $house->roof->A8 = $house->roof->A7;
                        return $house->roof->A1 + $house->roof->A2 + $house->roof->A3 + $house->roof->A4 + $house->roof->A5 + $house->roof->A6 + $house->roof->A7 + $house->roof->A8;
                    } else
   
                    //Toit forme D
                    if ($house->roof->idShape == 6) {
                        //Calcul de valeurs pour la surface
                        $house->roof->A1 = 2 * (($house->M1 - $house->M3) * sqrt(($house->M4/2)*($house->M4/2) + ($house->roof->height*$house->roof->height)));
                        $house->roof->A2 = 2 * (($house->M2 - $house->M4) * sqrt(($house->M3/2)*($house->M3/2) + ($house->roof->height*$house->roof->height)));
                        $house->roof->A3 = ($house->M2 - ($house->M4/2)) * sqrt(($house->M3/2)*($house->M3/2) + ($house->roof->height*$house->roof->height));
                        $house->roof->A4 = ($house->M1 - ($house->M3/2)) * sqrt(($house->M4/2)*($house->M4/2) + ($house->roof->height*$house->roof->height));
                        $house->roof->A5 = ($house->M3 / (2 * sqrt(($house->M4/2)*($house->M4/2) + ($house->roof->height*$house->roof->height)))) / 2;
                        $house->roof->A6 = $house->roof->A5;
                        $house->roof->A7 = ($house->M4 / (2 * sqrt(($house->M3/2)*($house->M3/2) + ($house->roof->height*$house->roof->height)))) / 2;
                        $house->roof->A8 = $house->roof->A7;
                        return 1.144 * ($house->roof->A1 + $house->roof->A2 + $house->roof->A3 + $house->roof->A4 + $house->roof->A5 + $house->roof->A6 + $house->roof->A7 + $house->roof->A8);
                        
                    }

                    break;

                //Forme en U
                case 4:
                    //Toit en pente
                    if ($house->roof->idShape == 1) {
                        $house->roof->A1 = ($house->M2 - $house->M6) * sqrt(($house->M1 * $house->M1)*($house->roof->height * $house->roof->height));
                        $house->roof->A2 = $house->roof->A1 / 2;
                        $house->roof->A3 = $house->M1 * sqrt(pow($house->M2 - $house->M6, 2) + pow($house->roof->height, 2));
                        $house->roof->A4 = ($house->M3 - $house->M4 - $house->M1) * sqrt(pow($house->M2 - $house->M6, 2) + pow($house->roof->height, 2));
                        $house->roof->A5 = $house->M4 * sqrt(pow($house->M2 - $house->M6, 2) + pow($house->roof->height, 2));
                        $house->roof->A6 = ($house->M2 - $house->M6) * sqrt(pow($house->M4, 2) + pow($house->roof->height, 2));
                        $house->roof->A7 = $house->M5 * sqrt(pow($house->M4, 2) + pow($house->roof->height, 2));
                        return $house->roof->A1 + $house->roof->A2 + $house->roof->A3 + $house->roof->A4 + $house->roof->A5 + $house->roof->A6 + $house->roof->A7;
                    } else
                        
                    //Toit forme B
                    if ($house->roof->idShape == 2 || $house->roof->idShape == 3 || $house->roof->idShape == 4) {
                               $house->roof->A1 = $house->M6 * sqrt(pow($house->M1/2, 2) + pow($house->roof->height, 2));
                               $house->roof->A2 = $house->roof->A1;
                               $house->roof->A3 = ($house->M2 - ($house->M6 / 2)) * sqrt(pow($house->M1/2, 2) + pow($house->roof->height, 2));
                               $house->roof->A4 = $house->roof->A3 / 2;
                               $house->roof->A5 = $house->roof->A4;
                               $house->roof->A6 = ($house->M1 / 2) * sqrt(pow(($house->M2 - $house->M6) / 2, 2) + pow($house->roof->height, 2));
                               $house->roof->A7 = $house->M1 * sqrt(pow(($house->M2 - $house->M6) / 2, 2) + pow($house->roof->height, 2));
                               $house->roof->A8 = $house->roof->A5;
                               $house->roof->A9 = ($house->M3 - $house->M1 - $house->M4) * sqrt(pow(($house->M2 - $house->M6) / 2, 2) + pow($house->roof->height, 2));
                               $house->roof->A10 = $house->roof->A9;
                               $house->roof->A11 = ($house->M4 / 2) * sqrt(pow(($house->M2 - $house->M6) / 2, 2) + pow($house->roof->height, 2));
                               $house->roof->A12 = $house->roof->A11 / 2;
                               $house->roof->A13 = ($house->M2 - $house->M6) / 4 * sqrt(pow($house->M4 / 2, 2) + pow($house->roof->height, 2));
                               $house->roof->A14 = ($house->M2 - $house->M6) / 2 * sqrt(pow($house->M4 / 2, 2) + pow($house->roof->height, 2));
                               $house->roof->A15 = $house->roof->A14 / 2;
                               $house->roof->A16 = $house->roof->A12;
                               $house->roof->A17 = $house->M5 * sqrt(pow($house->M4 / 2, 2) + pow($house->roof->height, 2));
                               $house->roof->A18 = $house->roof->A17;
                               return $house->roof->A1 + $house->roof->A2 + $house->roof->A3 + $house->roof->A4 + $house->roof->A5 + $house->roof->A6 + $house->roof->A7 + $house->roof->A8 + $house->roof->A9 + $house->roof->A10 + $house->roof->A11 + $house->roof->A12 + $house->roof->A13 + $house->roof->A14 + $house->roof->A15 + $house->roof->A16 + $house->roof->A17 + $house->roof->A18;
                               
                    } else
    
                    //Toit forme D
                    if ($house->roof->idShape == 6) {
                        $house->roof->A1 = $house->M6 * sqrt(pow($house->M1/2, 2) + pow($house->roof->height, 2));
                               $house->roof->A2 = $house->roof->A1;
                               $house->roof->A3 = ($house->M2 - ($house->M6 / 2)) * sqrt(pow($house->M1/2, 2) + pow($house->roof->height, 2));
                               $house->roof->A4 = $house->roof->A3 / 2;
                               $house->roof->A5 = $house->roof->A4;
                               $house->roof->A6 = ($house->M1 / 2) * sqrt(pow(($house->M2 - $house->M6) / 2, 2) + pow($house->roof->height, 2));
                               $house->roof->A7 = $house->M1 * sqrt(pow(($house->M2 - $house->M6) / 2, 2) + pow($house->roof->height, 2));
                               $house->roof->A8 = $house->roof->A5;
                               $house->roof->A9 = ($house->M3 - $house->M1 - $house->M4) * sqrt(pow(($house->M2 - $house->M6) / 2, 2) + pow($house->roof->height, 2));
                               $house->roof->A10 = $house->roof->A9;
                               $house->roof->A11 = ($house->M4 / 2) * sqrt(pow(($house->M2 - $house->M6) / 2, 2) + pow($house->roof->height, 2));
                               $house->roof->A12 = $house->roof->A11 / 2;
                               $house->roof->A13 = ($house->M2 - $house->M6) / 4 * sqrt(pow($house->M4 / 2, 2) + pow($house->roof->height, 2));
                               $house->roof->A14 = ($house->M2 - $house->M6) / 2 * sqrt(pow($house->M4 / 2, 2) + pow($house->roof->height, 2));
                               $house->roof->A15 = $house->roof->A14 / 2;
                               $house->roof->A16 = $house->roof->A12;
                               $house->roof->A17 = $house->M5 * sqrt(pow($house->M4 / 2, 2) + pow($house->roof->height, 2));
                               $house->roof->A18 = $house->roof->A17;
                               return 1.144 * ($house->roof->A1 + $house->roof->A2 + $house->roof->A3 + $house->roof->A4 + $house->roof->A5 + $house->roof->A6 + $house->roof->A7 + $house->roof->A8 + $house->roof->A9 + $house->roof->A10 + $house->roof->A11 + $house->roof->A12 + $house->roof->A13 + $house->roof->A14 + $house->roof->A15 + $house->roof->A16 + $house->roof->A17 + $house->roof->A18);
                               
                    }
                  
                    break;

                //Forme rect. décalé
                case 5:
                    //Toit en pente
                    if ($house->roof->idShape == 1) {
                        $house->roof->A1 = $house->M2 * sqrt(pow($house->M1, 2) + pow($house->roof->height, 2));
                        $house->roof->A2 = $house->M7 * sqrt(pow($house->M6, 2) + pow($house->roof->height, 2));
                        return $house->roof->A1 + $house->roof->A2;
                    } else

                    //Toit forme B
                    if ($house->roof->idShape == 2 || $house->roof->idShape == 3 || $house->roof->idShape == 4) {
                        $house->roof->A1 = $house->M2 * sqrt(pow($house->M1/2, 2) + pow($house->roof->height, 2));
                        $house->roof->A2 = $house->roof->A1;
                        $house->roof->A3 = $house->M7 * sqrt(pow($house->M6/2, 2) + pow($house->roof->height, 2));
                        $house->roof->A4 = $house->roof->A3;
                        return $house->roof->A1 + $house->roof->A2 + $house->roof->A3 + $house->roof->A4;
                    } else
                        
                    //Toit forme D
                    if ($house->roof->idShape == 6) {
                        $house->roof->A1 = $house->M2 * sqrt(pow($house->M1/2, 2) + pow($house->roof->height, 2));
                        $house->roof->A2 = $house->roof->A1;
                        $house->roof->A3 = $house->M7 * sqrt(pow($house->M6/2, 2) + pow($house->roof->height, 2));
                        $house->roof->A4 = $house->roof->A3;
                        return 1.144 * ($house->roof->A1 + $house->roof->A2 + $house->roof->A3 + $house->roof->A4);
                    }
                    break;

                //Forme en T
                case 6:
                    //Toit en pente
                    if ($house->roof->idShape == 1) {
                        $house->roof->A1 = $house->M2 * sqrt(pow($house->M1, 2) + pow($house->roof->height, 2));
                        $house->roof->A2 = $house->M5 * atan($house->roof->height / $house->M1);
                        return $house->roof->A1 + $house->roof->A2;
                    } else
                        
                    //Toit forme B
                    if ($house->roof->idShape == 2 || $house->roof->idShape == 3 || $house->roof->idShape == 4) {
                        $house->roof->A1 = $house->M2 * sqrt(pow($house->M1/2, 2) + pow($house->roof->height, 2));
                        $house->roof->A2 = $house->roof->A1;
                        $house->roof->A3 = $house->M5 * sqrt(pow($house->M4/2, 2) + pow($house->roof->height, 2));
                        $house->roof->A4 = $house->roof->A3;
                        return $house->roof->A1 + $house->roof->A2 + $house->roof->A3 + $house->roof->A4;
                    } else
  
                    //Toit forme D
                    if ($house->roof->idShape == 6) {
                        $house->roof->A1 = $house->M2 * sqrt(pow($house->M1/2, 2) + pow($house->roof->height, 2));
                        $house->roof->A2 = $house->roof->A1;
                        $house->roof->A3 = $house->M5 * sqrt(pow($house->M4/2, 2) + pow($house->roof->height, 2));
                        $house->roof->A4 = $house->roof->A3;
                        return 1.144 * ($house->roof->A1 + $house->roof->A2 + $house->roof->A3 + $house->roof->A4);
                    }
                    
                    break;

                default :
                    echo 'Forme de la maison non connue....';
                    break;
            }
        }
    }

    //Calcul de la surface du toit s'il est plat
    function getSurfaceFlatRoof($house) {

        switch ($house->idShape) {
            // Forme carré 
            case 1:
                return $house->M1 * $house->M1;
                break;
            // Forme rectangle 
            case 2:
                return $house->M1 * $house->M2;
                break;

            //Forme en L
            case 3:
                return ($house->M1 * $house->M4) + ($house->M2 - $house->M4) * $house->M3;
                break;

            //Forme en U
            case 4:
                return ($house->M1 * $house->M6) + ($house->M4 * $house->M5) + ($house->M2 - $house->M6) * $house->M3;
                break;

            //Forme rect. décalé
            case 5:
                return ($house->M1 * $house->M2) + $house->M6 * ($house->M7 - $house->M4) + ($house->M4 * $house->M5);
                break;

            //Forme en T
            case 6:
                return ($house->M1 * $house->M2) + ($house->M4 * $house->M5);
                break;

            default :
                echo 'Forme de la maison non connue....';
                break;
        }
    }
    
    //Fonction pour le calcul de la surface de référence énergétique (SRE)
    function getSRE($house) {
        switch ($house->idShape) {
            // Forme carré et rectangle
            case 1:
                $this->totalSurface = $house->M1 * $house->M1 * $house->floors;
                break;
            case 2:
                $this->totalSurface = $house->M1 * $house->M2;
                $this->totalSurface *= $house->floors;

                break;
            //Forme en L
            case 3:
                $this->totalSurface = ($house->M1 * $house->M4) + ($house->M2 - $house->M4) * $house->M3;
                $this->totalSurface *= $house->floors;
                break;
            //Forme en U
            case 4:
                $this->totalSurface = ($house->M1 * $house->M6) + ($house->M4 * $house->M5) + ($house->M2 - $house->M6) * $house->M3;
                $this->totalSurface *= $house->floors;
                break;
            //Forme rect. décalé
            case 5:
               $this->totalSurface = ($house->M1 * $house->M2) + $house->M6 * ($house->M7 - $house->M4) + ($house->M4 * $house->M5);
                $this->totalSurface *= $house->floors;
                break;
            //Forme en T
             case 6:
                $this->totalSurface = ($house->M1 * $house->M2) + ($house->M4 * $house->M5);
                $this->totalSurface *= $house->floors;
                break;
            
            default :
                echo 'Forme de la maison non connue....';
                break;
        }

        return $this->totalSurface;
    }

}
?>